﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace KineticWebCrystalExport
{
    public partial class CrystalExportPDF
    {
        public static bool CrystalToPDFExportToDisk(string PrintProgram, string FileName, string ExportPath, string ExportName, string RenderFormat, bool IsDebug = false)
        {
            try
            {
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E1"); }
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptRD = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E2"); }
                DataSet dsXML = new DataSet();
                dsXML.ReadXml(FileName);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E3"); }
                rptRD.Load(PrintProgram);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E4"); }
                rptRD.SetDataSource(dsXML);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E5"); }
                string strExportName = $"{ExportPath}{ExportName}.{RenderFormat}";
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", $"E5-1:{strExportName}"); }
                rptRD.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, strExportName);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E6"); }
            }
            catch (Exception ex)
            {
                TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "【" + System.Reflection.MethodBase.GetCurrentMethod().Name + "】" + ex.ToString());
                return false;
            }
            return true;
        }

        public static bool CrystalToPDFExportToDiskForKey(string PrintProgram, string FileName, string ExportPath, string ExportName, string RenderFormat, string Key1, string Company, string ServerName, string DatabaseName, string UserID, string Password, string POMailSubject, string POMailSend, string POMailReceied, string POMailCC, string POMailBody, bool POMailIsShipMark, bool IsSendMail, bool IsDebug = false)
        {
            try
            {
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "CrystalToPDFExportToDiskForKey.E1"); }
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptRD = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E2"); }
                rptRD.Load(PrintProgram);
                CrystalDecisions.Shared.TableLogOnInfo logonInfo = new CrystalDecisions.Shared.TableLogOnInfo();
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E3"); }
                foreach (CrystalDecisions.CrystalReports.Engine.Table table in rptRD.Database.Tables)
                {
                    logonInfo.ConnectionInfo.ServerName = ServerName;
                    logonInfo.ConnectionInfo.DatabaseName = DatabaseName;
                    logonInfo.ConnectionInfo.UserID = UserID;
                    logonInfo.ConnectionInfo.Password = Password;
                    table.ApplyLogOnInfo(logonInfo);
                }
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E4"); }
                rptRD.SetParameterValue("PONum", Key1);
                rptRD.SetParameterValue("Company", Company);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E5"); }
                string strExportName = $"{ExportPath}{ExportName}.{RenderFormat}";
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", $"E5-1:{strExportName}"); }
                //rptRD.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, strExportName);//越南文会乱码
                System.Drawing.Printing.PrinterSettings objPrintSet = new System.Drawing.Printing.PrinterSettings();
                objPrintSet.PrintToFile = true;
                objPrintSet.PrintFileName = strExportName;
                objPrintSet.PrinterName = "Microsoft Print to PDF";
                rptRD.PrintOptions.PrinterName = "Microsoft Print to PDF";
                rptRD.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;
                rptRD.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait;
                rptRD.PrintOptions.PrinterDuplex = CrystalDecisions.Shared.PrinterDuplex.Simplex;
                rptRD.PrintToPrinter(objPrintSet, new System.Drawing.Printing.PageSettings(), false);

                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E6"); }
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", $"POMailSubject:{POMailSubject}-POMailSend:{POMailSend}-POMailReceied:{POMailReceied}-POMailCC:{POMailCC}-POMailBody:{POMailBody}-IsSendMail{IsSendMail}"); }
                Int64 intTime = 0;
                while (intTime <= 60)
                {
                    if (System.IO.File.Exists(strExportName) == true)
                    {
                        System.IO.FileInfo objFileSize = new System.IO.FileInfo(strExportName);
                        if (objFileSize.Length > 0)
                        { break; }
                    }
                    System.Threading.Thread.Sleep(1000);
                    intTime += 1;
                }
                if (System.IO.File.Exists(strExportName) && IsSendMail == true)
                {
                    MailMessage message = new MailMessage();
                    SmtpClient client = new SmtpClient();
                    client = new SmtpClient
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        EnableSsl = false,
                        Host = "smtp.exmail.qq.com",//"10.0.0.17",
                        Port = 25,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential("Administrator@nadfinlo.com.cn", "ZnjYyEKRrp6i3QKf")// szmis01e@nadfinlo.com.cn", "12345")  SSzmis2019@
                    };
                    message = new MailMessage
                    {
                        Priority = MailPriority.High,
                        From = new MailAddress("Administrator@nadfinlo.com.cn", POMailSend, Encoding.GetEncoding(0x3a8)),
                        ReplyTo = new MailAddress(POMailSend, POMailSend, Encoding.GetEncoding(0x3a8))
                    };
                    if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E7"); }
                    message.To.Add(POMailReceied);
                    if (POMailCC.Length > 0)
                    {
                        message.CC.Add(POMailCC);
                    }
                    if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E8"); }
                    message.SubjectEncoding = Encoding.GetEncoding(0x3a8);
                    message.IsBodyHtml = true;
                    message.BodyEncoding = Encoding.GetEncoding(0x3a8);
                    message.Subject = POMailSubject;
                    string str2 = POMailBody.Replace("\n", "<br>").Replace(" ", "&nbsp;");
                    message.Body = str2;
                    message.Attachments.Add(new Attachment(strExportName));
                    if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E9"); }
                    client.Send(message);
                    if (message.Attachments != null && message.Attachments.Count > 0)
                    {
                        foreach (System.Net.Mail.Attachment obj in message.Attachments)//清除附件缓存
                        {
                            obj.Dispose();
                        }
                    }
                    if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "E10"); }
                }
            }
            catch (Exception ex)
            {
                TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "【" + System.Reflection.MethodBase.GetCurrentMethod().Name + "】" + ex.ToString());
                return false;
            }
            return true;
        }

        public static bool CrystalToPDFPrintToPrinter(string PrintProgram, string FileName, string ExportPath, string ExportName, string RenderFormat, bool IsDebug = false)
        {
            try
            {

                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P1"); }
                CrystalDecisions.CrystalReports.Engine.ReportDocument rptRD = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P2"); }
                DataSet dsXML = new DataSet();
                dsXML.ReadXml(FileName);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P3"); }
                rptRD.Load(PrintProgram);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P4"); }
                rptRD.SetDataSource(dsXML);
                string strExportName = $"{ExportPath}{ExportName}.{RenderFormat}";
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P5"); }
                System.Drawing.Printing.PrinterSettings objPrintSet = new System.Drawing.Printing.PrinterSettings();
                objPrintSet.PrintToFile = true;
                objPrintSet.PrintFileName = strExportName;
                objPrintSet.PrinterName = "Microsoft Print to PDF";
                rptRD.PrintOptions.PrinterName = "Microsoft Print to PDF";
                rptRD.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;
                rptRD.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait;
                rptRD.PrintOptions.PrinterDuplex = CrystalDecisions.Shared.PrinterDuplex.Simplex;
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P6"); }
                rptRD.PrintToPrinter(objPrintSet, new System.Drawing.Printing.PageSettings(), false);
                if (IsDebug) { TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "P7"); }
            }
            catch (Exception ex)
            {
                TxtConvert.WriteToTxt($@"\ErrorLogImport{DateTime.Now.ToString("yyyy-MM")}.txt", "【" + System.Reflection.MethodBase.GetCurrentMethod().Name + "】" + ex.ToString());
                return false;
            }
            return true;
        }
    }
}
